//
//  IAAAuthCheComCCell.m
//  iPadAuditAuthorise
//
//  Created by XiangDongMBP on 16/10/10.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "IAAAuthCheComCCell.h"
#import "IAAAuthCheckModel.h"

@implementation IAAAuthCheComCCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.upBgView.layer.borderColor = IAA_COLOR_0xE0E0E0(1.0).CGColor;
    self.upBgView.layer.masksToBounds = YES;
    
    NSArray<UIButton *> *cameraArray = @[self.oneCameraBtn, self.twoCameraBtn, self.threeCameraBtn, self.fourCameraBtn];
    [cameraArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.tag = idx;
        [obj addTarget:self action:@selector(cameraBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    }];
}

- (void)setSelected:(BOOL)selected {
    self.upBgView.layer.borderColor = selected ? IAA_COLOR_0x129CFF(1.0).CGColor : IAA_COLOR_0xE0E0E0(1.0).CGColor;
}

- (void)cameraBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(IAAAuthCheComCCell:cameraBtnTapped:)]) {
        [self.delegate IAAAuthCheComCCell:self cameraBtnTapped:btn.tag];
    }
}

// 配置界面
- (void)configViewWithData:(IAAAuthCheckDetailModel *)model sortItem:(IAAAuthCheckSortItem *)sortItem type:(IAAAuthCheComCCellType)type {
    self.model = sortItem;
    self.type = type;
    
    self.upBgView.layer.borderWidth = (type == IAAAuthCheComCCellTypeCircle) ? 1 : 0;
    self.upBgView.layer.cornerRadius = (type == IAAAuthCheComCCellTypeCircle) ? 6 : 0;
    
    self.topConstraint.constant = (type == IAAAuthCheComCCellTypeAllScreen) ? 0 : 12;
    self.leadingConstraint.constant = (type == IAAAuthCheComCCellTypeAllScreen) ? 0 : 12;
    self.trailingConstraint.constant = (type == IAAAuthCheComCCellTypeAllScreen) ? 0 : 12;
    self.bottomLabelHeightConstraint.constant = (type == IAAAuthCheComCCellTypeAllScreen) ? 0 : 17;
    
    self.titleUpHeightConstraint.constant = (type == IAAAuthCheComCCellTypeCircle) ? 0 : 17;
    self.titleBottomHeightConstraint.constant = (type == IAAAuthCheComCCellTypeCircle) ? 0 : 17;
    self.idUpWidthConstraint.constant = (type == IAAAuthCheComCCellTypeCircle) ? 0 : 60;
    self.idBottomWidthConstraint.constant = (type == IAAAuthCheComCCellTypeCircle) ? 0 : 60;
    
    self.idUpLabel.text = @"通过";
    self.idBottomLabel.text = @"通过";
    if ([sortItem haveImageType:28] || [sortItem haveImageType:29]) {
        self.titleUpLabel.text = [NSString stringWithFormat:@"%@%@", @"人脸图像及识别结果：", model.totalFaceResult];
        self.titleBottomLabel.text = [NSString stringWithFormat:@"%@%@", @"身份联网核查结果：", model.agentLwhcResult];
        self.bottomLabel.text = @"代办人信息";
    } else {
        self.titleUpLabel.text = [NSString stringWithFormat:@"%@%@", @"人脸图像及识别结果：", model.totalFaceResult];
        self.titleBottomLabel.text = [NSString stringWithFormat:@"%@%@", @"身份联网核查结果：", model.mainLwhcResult];
        self.bottomLabel.text = @"主办人信息";
    }
    
    NSArray *imgArray = @[self.oneImgView, self.twoImgView, self.threeImgView, self.fourImgView];
    NSArray *labelArray = @[self.oneLabel, self.twoLabel, self.threeLabel, self.fourLabel];
    NSArray *cameraArray = @[self.oneCameraBtn, self.twoCameraBtn, self.threeCameraBtn, self.fourCameraBtn];
    
    for (NSInteger i = 0; i < imgArray.count; i++) {
        UIImageView *imageView = imgArray[i];
        UILabel *label = labelArray[i];
        UIButton *cameraBtn = cameraArray[i];
        if (i < sortItem.imgArray.count) {
            IAAAuthCheckImgItem *obj = sortItem.imgArray[i];
            imageView.image = obj.imageObject;
            if (type == IAAAuthCheComCCellTypeCircle || type == IAAAuthCheComCCellTypeAllScreen) {
                label.text = nil;
                cameraBtn.hidden = YES;
            } else if(type == IAAAuthCheComCCellTypeRect) {
                label.text = obj.imageName.length ? obj.imageName : @" ";
                cameraBtn.hidden = !(obj.isReTakePicture || obj.isForce);
            } else {
                label.text = nil;
                cameraBtn.hidden = YES;
            }
            if (obj.ufid.length && !obj.imageBase64.length) {
                NSString *requestStr = [IAAUrlProtocolMerge appFileImgQueryImg];
                NSDictionary *pars = @{@"ufid"   : obj.ufid,
                                       @"osName" : @"iOS",
                                       @"token"  : IAA_APP_DELEGATE.token};
                [XDAFHttpRequest POST:requestStr parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    if ([responseObject[@"resultCode"] isEqualToString:IAA_RESULT_CODE_SUC]) {
                        if (!obj.imageBase64.length) {
                            obj.imageBase64 = responseObject[@"imageBase64"];
                        }
                        imageView.image = obj.imageObject;
                    }
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    ;
                }];
            }
        } else {
            imageView.image = nil;
            label.text = nil;
            cameraBtn.hidden = YES;
        }
    }
}

@end
